package com.qn.meta.controlle;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil;
import com.qn.common.base.BaseControlle;
import com.qn.common.base.model.RequestPage;
import com.qn.common.base.model.ResponseData;
import com.qn.common.base.model.ResponseMode;
import com.qn.core.resources.ResourcesSession;
import com.qn.meta.entity.User;
import com.qn.meta.service.AuthenticationService;
import com.qn.meta.util.VisitorUtil;
import com.qn.meta.vo.LoginVo;
import com.qn.meta.vo.SessionListVo;
import com.qn.meta.vo.UserLoginVo;
import com.qn.meta.vo.UserRegisterVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * qingniu-cloud user source analysis
 *
 * @Author: TXG
 * @Date: 2024/6/11
 * @Version V0.0.2
 */
@Tag(name = "用户鉴权")
@SaCheckLogin
@RestController
@RequestMapping("authentication")
public class AuthenticationController extends BaseControlle {
    @DubboReference(version = "1.0.0")
    AuthenticationService service;

    @SaIgnore
    @Operation(summary = "用户登录")
    @PostMapping("login")
    public ResponseData<UserLoginVo> getUserById(@Validated @RequestBody LoginVo loginVo,
                                                 HttpServletRequest request) {
        UserLoginVo userLogin  =service.login(loginVo);
        StpUtil.login(userLogin.getId(), new SaLoginModel()
                .setDevice(loginVo.getDevice())
                .setIsLastingCookie(loginVo.getIsLastingCookie()));
        StpUtil.getSession().set(ResourcesSession.USER_NAME, userLogin.getName());
        StpUtil.getSession().set(ResourcesSession.CLIENT_IP, VisitorUtil.getIp(request));
        StpUtil.getSession().set(ResourcesSession.CLIENT_LANGUAGE, VisitorUtil.getLanguage(request));
        StpUtil.getSession().set(ResourcesSession.USER_AGENT, VisitorUtil.getUserAgents(request));
        userLogin.setToken(StpUtil.getTokenValue());
        return successD(userLogin);
    }

    @SaIgnore
    @Operation(summary = "用户注册")
    @PostMapping("register")
    public ResponseMode register(@Validated @RequestBody UserRegisterVo registerVo) {
        User model = new User();
        model.setName(registerVo.getName());
        model.setPhone(registerVo.getPhone());
        model.setEmail(registerVo.getEmail());
        model.setPassword(registerVo.getPassword());
        service.register(model);
        return success();
    }


    
    @SaIgnore
    @Operation(summary = "退出登录")
    @GetMapping("logOut")
    public ResponseMode logOut() {
        service.logOut();
        return success();
    }

    @Operation(summary = "强制下线")
    @GetMapping("kickout/{id}")
    public ResponseMode kickout(@PathVariable("id") @Parameter(name = "用户ID") Integer id) throws Exception {
        service.kickout(id);
        return success();
    }

    @Operation(summary = "会话列表")
    @PostMapping("getPage")
    public ResponseData<List<SessionListVo>> getPage(@RequestBody RequestPage<SessionListVo> rpage) {
        return successD(service.getSessionList(rpage));
    }
}